package com.moyuwanjia.Dao.Impl;

import com.moyuwanjia.Dao.IAccountDao;
import com.moyuwanjia.entity.Account02;
import com.moyuwanjia.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 摸鱼丸加~
 * @create 2022-04-24 14:38
 */
public class AccountDaoImpl implements IAccountDao {
    private QueryRunner queryRunner;
    private ConnectionUtils connectionUtils;

    public void setRunner(QueryRunner runner){
        this.queryRunner = runner;
    }
    public void setConnectionUtils(ConnectionUtils connectionUtils){
        this.connectionUtils = connectionUtils;
    }


    public List<Account02> findAll() {
        try {
            return queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02",
                    new BeanListHandler<Account02>(Account02.class));
        }catch (Exception exception){
            throw new RuntimeException(exception);
        }
    }

    public Account02 findAccountById(Integer id) {
        try {
            return queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02 where id = ?",new BeanHandler<Account02>(Account02.class),id);
        }catch (Exception exception){
             throw new RuntimeException(exception);
        }
    }

    public Integer saveAccount(Account02 account02) {
        try {
            return queryRunner.update(connectionUtils.getThreadConnection(),"insert into account02(name,money) values(?,?)",account02.getName(),account02.getMoney());
        } catch (SQLException sqlException) {
            throw  new RuntimeException(sqlException);
        }
    }

    public Integer updateAccount(Account02 account02) {
        try {
            return queryRunner.update(connectionUtils.getThreadConnection(),"update account02 set money=? where name = ?",account02.getMoney(),account02.getName());
        } catch (SQLException sqlException) {
            throw new RuntimeException(sqlException);
        }
    }

    public Integer deleteAccountById(Integer id) {
        try {
            return queryRunner.update(connectionUtils.getThreadConnection(),"delete from account02 where id = ?",id);
        } catch (SQLException sqlException) {
            throw new RuntimeException(sqlException);
        }
    }

    /**
     * 根据名称查询对应数据，只能返回一条数据
     * @param accountName
     * @return
     */
    @Override
    public Account02 findAccountByName(String accountName) {
        try {
            final List<Account02> query = queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02 where name = ?",
                    new BeanListHandler<Account02>(Account02.class),
                    accountName);
            if(CollectionUtils.isEmpty(query) || query.size()==0) return null;
            if(query.size() > 1) throw new RuntimeException("数据存在问题，结果集不唯一");
            return query.get(0);
        }catch (SQLException exception){
           throw new RuntimeException(exception);
        }
    }
}
